package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.control.labels.LabelData;
import com.wgts.wgts.mvp.model.ISearchResultFragmentModel;
import com.wgts.wgts.mvp.presenter.IShopClassChildActivityPresenter;
import com.wgts.wgts.mvp.view.IShopClassChildActivityView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.SearchShopListRequest;
import com.wgts.wgts.netmodle.resp.ScreenEntity;
import com.wgts.wgts.netmodle.resp.SearchShopResultEntity;

import java.util.ArrayList;
import java.util.List;

/*
 *
 *
 *   data:2018/10/716:59
 *
 */public class ShopClassChildActivityPresenter implements IShopClassChildActivityPresenter {
     private IShopClassChildActivityView view;
     private ISearchResultFragmentModel model;
     private SearchShopListRequest request;

     //类型筛选数据
    private List<LabelData> typeScreenDate;
    //品牌筛选数据
    private List<LabelData> brandScreenDate;
    private int pageIndex = 1;
    private int pageSize = 20;


    public ShopClassChildActivityPresenter(IShopClassChildActivityView view, ISearchResultFragmentModel model) {
        this.view = view;
        this.model = model;
    }

    @Override
    public void getShopList(int typeId, String name, Integer brandId, Double minPrice, Double maxPrice,
                            String order, String sort) {
        pageIndex = 1;
        request = new SearchShopListRequest();
        request.pageIndex = pageIndex;
        request.pageSize = pageSize;
        request.typeId = typeId;
        request.name = name;
        request.brandId = brandId;
        request.minPrice = minPrice;
        request.maxPrice = maxPrice;
        request.order = order;
        request.sort = sort;
        model.getShopList(request, new HttpResultSubscriber<SearchShopResultEntity>() {
            @Override
            public void onSuccess(SearchShopResultEntity entity) {
                if (entity.list.size() < pageSize){
                    view.getShopList(entity, true);
                }else {
                    view.getShopList(entity, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void getScreen(final int typeId) {
        model.getScreen(typeId, new HttpResultSubscriber<ScreenEntity>() {
            @Override
            public void onSuccess(ScreenEntity screenEntity) {
                typeScreenDate = new ArrayList<>();
                brandScreenDate = new ArrayList<>();
                for (int i = 0; i <= screenEntity.typeList.size(); i++){
                    LabelData labelData = new LabelData();
                    if (i == 0){
                        labelData.setName("全部");
                        labelData.setId(typeId);
                    }else {
                        labelData.setName(screenEntity.typeList.get(i-1).typeName);
                        labelData.setId(Integer.parseInt(screenEntity.typeList.get(i-1).typeId));
                    }
                    typeScreenDate.add(labelData);
                }
                for (int i = 0; i < screenEntity.brandList.size(); i++){
                    LabelData labelData = new LabelData();
                    labelData.setName(screenEntity.brandList.get(i).name);
                    labelData.setId(Integer.parseInt(screenEntity.brandList.get(i).id));
                    brandScreenDate.add(labelData);
                }
                view.screenDate(typeScreenDate, brandScreenDate);
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }

    @Override
    public void loadMore(int typeId, String name, Integer brandId, Double minPrice, Double maxPrice, String order, String sort) {
        request = new SearchShopListRequest();
        request.pageIndex = pageIndex;
        request.pageSize = pageSize;
        request.typeId = typeId;
        request.name = name;
        request.brandId = brandId;
        request.minPrice = minPrice;
        request.maxPrice = maxPrice;
        request.order = order;
        request.sort = sort;
        model.getShopList(request, new HttpResultSubscriber<SearchShopResultEntity>() {
            @Override
            public void onSuccess(SearchShopResultEntity entity) {
                if (entity.list.size() < pageSize){
                    view.loadMore(entity, true);
                }else {
                    view.loadMore(entity, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }
}
